<template>
  <div>
    <div class="My_title">
      <div class="bgimg"></div>
      <!--未登录页面 -->
      <div v-if="!user" class="userinfo">
        <div class="avatar">
          <img src="http://liufusong.top:8080/img/profile/avatar.png" alt="" />
        </div>
        <div class="user-login">
          <span class="username">youke</span>
          <van-button
          class='not-btn'
            color="#21b97a"
            block
            size="normal"
            type="primary"
            to="/login"
            >去登陆</van-button
          >
        </div>
      </div>
      <!--！未登录页面 -->

      <!--已登录页面 -->
      <div v-else class="userinfo">
        <div class="avatar">
          <img :src="'http://liufusong.top:8080'+userinfoList.avatar" alt="" />
        </div>
        <div class="user-login">
          <span class="username">{{userinfoList.nickname}}</span>
          <van-button
            class="btn-auth"
            color="#21b97a"
            block
            size="normal"
            type="primary"
            @click="outlogin"
            >退出</van-button
          >
          <div class="userEdit">编辑个人资料<van-icon name="play" /></div>
        </div>
      </div>
      <!--！ 已登录页面 -->
    </div>

    <!-- 分类导航 -->
    <div>
      <van-grid :column-num="3">
        <van-grid-item icon="photo-o" text="我的收藏" to='/favorate' />
        <van-grid-item icon="photo-o" text="我的出租" to='/myrenout'/>
        <van-grid-item icon="photo-o" text="看房记录" />
        <van-grid-item icon="photo-o" text="成为房主" />
        <van-grid-item icon="photo-o" text="个人资料" />
        <van-grid-item icon="photo-o" text="联系我们" />
      </van-grid>
      <router-view/>
    </div>
    <!-- ！分类导航 -->

    <!-- 广告 -->
    <div class="ad">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="" />
    </div>
  </div>
</template>

<script>
import { Dialog, Toast } from 'vant'
import { mapState } from 'vuex'
import { userInfo } from '@/api/user.js'
export default {
  data () {
    return { userinfoList: {} }
  },

  created () {
    this.userinfo()
  },

  methods: {
    // 获取用户信息
    async userinfo () {
      try {
        const res = await userInfo()
        this.userinfoList = res
        console.log(this.userinfoList)
      } catch (err) {
        Toast.fail('获取信息失败')
      }
    },
    outlogin () {
      Dialog.confirm({
        title: '好客租房',
        message: '是否确认退出该账号'
      })
        .then(() => {
          this.$store.commit('infoRemoveStorage')
          this.$router.push('/login')
        })
        .catch(() => {})
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style scoped lang="less">
//登录头部
.My_title {
  position: relative;
  width: 100%;
  min-height: 300px;
  .bgimg {
    height: 191px;
    // background-color: aqua;
    background-image: url(http://liufusong.top:8080/img/profile/bg.png);
    background-size: 100% 100%;
  }
  // 用户信息
  .userinfo {
    position: absolute;
    background: #fff;
    width: 280px;
    height: 165px;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    box-shadow: 0 0 10px 3px #ddd;
    margin: 50px auto 0;
    padding: 0 20px;
    text-align: center;
    font-size: 13px;
    // 头像
    .avatar {
      position: absolute;
      top: -50px;
      left: 125px;
      border-radius: 50%;
      width: 70px;
      height: 70px;
      border: 5px solid #f5f5f5;
      display: inline-block;
      box-shadow: 0 2px 2px #bdbdbd;
      img {
        width: 100%;
      }
    }
    // 按钮登录
    .user-login {
      margin-top: 50px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .username {
        font-size: 14px;
      }
      .not-btn {
        margin-top: 15px;
        width: 71px;
        height: 30px;
        padding: 0;
        border-radius: 10%;
        // height: 58px;
      }
      .btn-auth {
        margin: 10px 0;
        width: 54px;
        height: 20px;
        padding: 0;
        font-size: 12px;
        border-radius: 30px;
      }
    }
  }
  .userEdit {
    color: #999;
    font-size: 12px;
    margin-top: 20px;
  }
}

// 广告
.ad {
  // box-sizing: border-box;
  width: 375px;
  height: 87px;
  text-align: center;
  img {
    width: 92%;
    height: 100%;
  }
}
</style>
